package com.fh.modules.dbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.dbms.entity.DbmsTableIndex;
import com.fh.modules.dbms.entity.DbmsTableIndexColumn;
import com.fh.modules.dbms.mapper.DbmsTableIndexColumnMapper;
import com.fh.modules.dbms.service.IDbmsTableIndexColumnService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author qiujunda
 * @Date 23/07/2020
 * @Description
 */
@Service
public class DbmsTableIndexColumnServiceImpl extends AbstractServiceImpl<DbmsTableIndexColumnMapper, DbmsTableIndexColumn> implements IDbmsTableIndexColumnService {

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public boolean save(DbmsTableIndexColumn entity) {
        return super.save(entity);
    }
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public boolean deleteByTableId(Long indexId){
        UpdateWrapper<DbmsTableIndexColumn> updateWrapper=new UpdateWrapper<DbmsTableIndexColumn>();
        updateWrapper.set("update_time",new Date());
        updateWrapper.set("del_flag",1);
        updateWrapper.eq("index_id",indexId);
        updateWrapper.eq("del_flag",0);
        return this.update(updateWrapper);
    }

    @Override
    public List<DbmsTableIndexColumn> queryTableIndexColumns(Long indexId){
        QueryWrapper<DbmsTableIndexColumn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("index_id", indexId).eq("del_flag",0);
        queryWrapper.orderByAsc("column_position");
        return this.list(queryWrapper);
    }
}
